//
//  PosterView.m
//  HWSGW
//
//  Created by avachen on 15/11/7.
//  Copyright © 2015年 luxuanrui. All rights reserved.
//

#import "PosterView.h"
#import "UIViewExt.h"
#import "PosterCollectionView.h"
#import "IndexCollectionView.h"
#import "Movie.h"

#define kHeaderViewHeight 100
#define kFooterViewHeight 35

@implementation PosterView
{
    UIControl *_maskView; //半透明的遮盖视图
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //1、创建头部视图
        [self _createHeaderView];
        //2、创建海报视图
        [self _createPosterCollectionView];
        
        [self _createIndexCollectionView];
        
        [self _createFooterView];
        
        //使用KVO观察_posterCollectionView、_indexCollectionView大图、小图2个对象的currentItem属性值的变化
        //[被观察对象 addOberve：观察对象 参数2：属性名]
        [_posterCollectionView addObserver:self forKeyPath:@"currentItem" options:NSKeyValueObservingOptionNew context:NULL];
        [_indexCollectionView addObserver:self forKeyPath:@"currentItem" options:NSKeyValueObservingOptionNew context:NULL];
    }
    return self;
}
- (void)_createFooterView
{
    _footerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, self.height - kFooterViewHeight, kScreenWidth, kFooterViewHeight)];
    _footerLabel.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"poster_title_home.png"]];
    _footerLabel.font = [UIFont systemFontOfSize:16];
    _footerLabel.textColor = [UIColor whiteColor];
    _footerLabel.textAlignment = NSTextAlignmentCenter;
    _footerLabel.text = @"电影标题";
    [self addSubview:_footerLabel];
    
    
}
#pragma mark - KVO 观察者方法
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
    if ([keyPath isEqualToString:@"currentItem"]) {
        NSNumber *newValue = [change objectForKey:@"new"];
        NSInteger item = [newValue integerValue];
        
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:item inSection:0];
        
        if (object == _posterCollectionView && _indexCollectionView.currentItem != item) {
            _indexCollectionView.currentItem = item;
            
            [_indexCollectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
        }else if (object == _indexCollectionView && _posterCollectionView.currentItem != item){
            _posterCollectionView.currentItem = item;
            
            [_posterCollectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
        }
        Movie *movie = self.data[item];
        _footerLabel.text = movie.title;
        
        
    }
}
- (void)_createIndexCollectionView{
    _indexCollectionView = [[IndexCollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kHeaderViewHeight)];
    _indexCollectionView.pageWidth = 75;
    [_headerView addSubview:_indexCollectionView];
    
    
}
- (void)setData:(NSArray *)data
{
    if (_data != data) {
        _data = data;
        [_posterCollectionView setData:data];
        
        [_indexCollectionView setData:data];
        
        if (data.count > 0) {
            Movie *movie = [data objectAtIndex:0];
            _footerLabel.text = movie.title;
            
        }
        
    }
}
//2、创建海报视图
- (void)_createPosterCollectionView{
    _posterCollectionView = [[PosterCollectionView alloc] initWithFrame:CGRectMake(0, 30, kScreenWidth, self.height - 30 - kFooterViewHeight)];
    _posterCollectionView.backgroundColor = [UIColor clearColor];
    _posterCollectionView.pageWidth = 220;
    
    [self insertSubview:_posterCollectionView belowSubview:_headerView];
    
}
//1、创建头部视图
- (void)_createHeaderView
{
    //1、创建父视图
    _headerView = [[UIView alloc] initWithFrame:CGRectMake(0, -kHeaderViewHeight, kScreenWidth, 130)];
    _headerView.backgroundColor = [UIColor clearColor];
    [self addSubview:_headerView];
    
    //2、填充拉伸图片
    UIImage *img = [UIImage imageNamed:@"indexBG_home"];
    img = [img stretchableImageWithLeftCapWidth:0 topCapHeight:2]; //纵向拉伸
    
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0  , kScreenWidth, 130)];
    imgView.image = img;
    [_headerView addSubview:imgView];
    
    //3、创建箭头按钮
    UIButton *arrowsButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [arrowsButton setImage:[UIImage imageNamed:@"down_home.png"] forState:UIControlStateNormal];
    [arrowsButton setImage:[UIImage imageNamed:@"up_home.png"] forState:UIControlStateSelected];
    arrowsButton.tag = 100;
    
    arrowsButton.frame = CGRectMake((kScreenWidth - 10) / 2, 130 - 20, 20, 15);
    [arrowsButton addTarget:self action:@selector(arrowsAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [_headerView addSubview:arrowsButton];
    
    //4、创建向下轻扫的手势
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
    swipe.direction = UISwipeGestureRecognizerDirectionDown;
    
    [self addGestureRecognizer:swipe];
    
}
#pragma mark - 隐藏、显示头部视图
//显示头部视图
- (void)_showHeaderView{
    [UIView animateWithDuration:0.4 animations:^{
        
        _headerView.transform = CGAffineTransformMakeTranslation(0, kHeaderViewHeight);
    }];
    if (_maskView == nil) {
        
        _maskView = [[UIControl alloc] initWithFrame:self.bounds];
        _maskView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
        [_maskView addTarget:self action:@selector(maskAction:) forControlEvents:UIControlEventTouchUpInside];
        [self insertSubview:_maskView belowSubview:_headerView];
    }
    
    UIButton *arrowsButton = (UIButton *)[_headerView viewWithTag:100];
    arrowsButton.selected = YES;
    _maskView.hidden = NO;
    
}
//隐藏头部视图
- (void)_hideHeaderView{
    //动画隐藏头部视图
    [UIView animateWithDuration:0.4 animations:^{
        
        _headerView.transform = CGAffineTransformIdentity;
        ;
    }];
    UIButton *arrowsButton = (UIButton *)[_headerView viewWithTag:100];
    arrowsButton.selected = NO;
    _maskView.hidden = YES;
}
#pragma mark - 事件方法
//轻扫的方法
- (void)swipe:(UISwipeGestureRecognizer *)swipe{
    
    
    [self _showHeaderView];
    
    
}
//箭头按钮点击事件
- (void)arrowsAction:(UIButton *)button{
    
    if (button.selected == NO) {
        //动画显示头部视图
        [self _showHeaderView];
    }else{
        [self _hideHeaderView];
        
        
    }
    
    
    
}
//遮盖视图点击方法
- (void)maskAction:(UIControl *)control{
    [self _hideHeaderView];
    
}
@end
